package com.ruoyi.web.controller.fs;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.fs.domain.FsCapital;
import com.ruoyi.fs.domain.FsCase;
import com.ruoyi.fs.domain.FsCaseExpress;
import com.ruoyi.fs.service.IFsCaseExpressService;
import com.ruoyi.fs.service.IFsCaseService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.fs.domain.FsExpressInfo;
import com.ruoyi.fs.service.IFsExpressInfoService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 快递信息Controller
 *
 * @author hero
 * @date 2024-10-28
 */
@RestController
@RequestMapping("/fs/express")
public class FsExpressInfoController extends BaseController {
    @Autowired
    private IFsExpressInfoService fsExpressInfoService;
    @Autowired
    private IFsCaseExpressService fsCaseExpressService;
    @Autowired
    private IFsCaseService fsCaseService;

    /**
     * 查询快递信息列表
     */
    @PreAuthorize("@ss.hasPermi('fs:case:list')")
    @GetMapping("/list")
    public TableDataInfo list(FsExpressInfo fsExpressInfo) {
        startPage();
        List<FsExpressInfo> list = fsExpressInfoService.selectFsExpressInfoList(fsExpressInfo);
        return getDataTable(list);
    }

    /**
     * 导出快递信息列表
     */
    @PreAuthorize("@ss.hasPermi('fs:case:export')")
    @Log(title = "快递信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, FsExpressInfo fsExpressInfo) {
        List<FsExpressInfo> list = fsExpressInfoService.selectFsExpressInfoList(fsExpressInfo);
        ExcelUtil<FsExpressInfo> util = new ExcelUtil<FsExpressInfo>(FsExpressInfo.class);
        util.exportExcel(response, list, "快递信息数据");
    }

    /**
     * 获取快递信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('fs:case:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(fsExpressInfoService.selectFsExpressInfoById(id));
    }

    /**
     * 新增快递信息
     */
    @PreAuthorize("@ss.hasPermi('fs:case:add')")
    @Log(title = "快递信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody FsExpressInfo fsExpressInfo) {
        if (fsExpressInfo.getGxsj() == null) {
            fsExpressInfo.setGxsj(new Date());
        }
        if (fsExpressInfo.getId() == null) {
            fsExpressInfoService.insertFsExpressInfo(fsExpressInfo);
        } else {
            fsExpressInfoService.updateFsExpressInfo(fsExpressInfo);
        }
        FsCaseExpress caseExpress = new FsCaseExpress();
        caseExpress.setCaseId(fsExpressInfo.getCaseId());
        caseExpress.setExpressId(fsExpressInfo.getId());
        List<FsCaseExpress> list = fsCaseExpressService.selectFsCaseExpressList(caseExpress);
        if (list.isEmpty()) {
            fsCaseExpressService.insertFsCaseExpress(caseExpress);
        }
        if (fsExpressInfo.getYwlx() == 1) {
            FsCase fsCase = fsCaseService.selectFsCaseById(fsExpressInfo.getCaseId());
            fsCase.setLafs(1);
            fsCase.setIfPost(1);
            fsCaseService.updateFsCase(fsCase);
        }
        return success();
    }

    /**
     * 修改快递信息
     */
    @PreAuthorize("@ss.hasPermi('fs:case:edit')")
    @Log(title = "快递信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody FsExpressInfo fsExpressInfo) {
        return toAjax(fsExpressInfoService.updateFsExpressInfo(fsExpressInfo));
    }

    /**
     * 删除快递信息
     */
    @PreAuthorize("@ss.hasPermi('fs:case:remove')")
    @Log(title = "快递信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(fsExpressInfoService.deleteFsExpressInfoByIds(ids));
    }

    /**
     * 查询诉讼主体
     */
    @PreAuthorize("@ss.hasPermi('fs:case:list')")
    @GetMapping("/getByKddh/{dh}")
    public AjaxResult getByKddh(@PathVariable("dh") String dh) {
        FsExpressInfo query = new FsExpressInfo();
        query.setKddh(dh);
        List<FsExpressInfo> list = fsExpressInfoService.selectFsExpressInfoList(query);
        if (list.isEmpty()) {
            return success();
        } else {
            return success(list.get(0));
        }
    }
}
